package com.mijie.core;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class CoreFilter implements Filter {

	private static Logger logger = Logger.getLogger(CoreFilter.class);
	private boolean isWebApp = false;
	private String charset = "UTF-8";
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		
		request.setCharacterEncoding(charset);
		response.setCharacterEncoding(charset);
		
		if(isWebApp){
			Map<String,Object> searchBundle = parseSearchBundle(request);
			request.setAttribute("searchBundle", searchBundle);
		}
		
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
		request.setAttribute("ctx", basePath);
		
		try{
			chain.doFilter(request, response);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
		}
		
	}

	private Map<String,Object> parseSearchBundle(HttpServletRequest request){
		Enumeration<String> parameterKeys = request.getParameterNames();
		Map<String,Object> searchBundle = new HashMap<String,Object>();
		String key = null;
		String value = null;
		if(parameterKeys!=null){
			while(parameterKeys.hasMoreElements()){
				key = parameterKeys.nextElement();
				if(key.charAt(0)=='_'){
					value = request.getParameter(key);
					if(value!=null && !value.isEmpty() && !value.equals("-999")){
						searchBundle.put(key.substring(1), value);
						if(logger.isDebugEnabled()) logger.info("####searchBundle get from getParameter and put("+key.substring(1)+","+value+")");
					}
				}
			}
		}
		parameterKeys = request.getAttributeNames();
		if(parameterKeys!=null){
			while(parameterKeys.hasMoreElements()){
				key = parameterKeys.nextElement();
				if(key.charAt(0)=='_'){
					value = (String) request.getAttribute(key);
					if(value!=null && !value.isEmpty() && !value.equals("-999")){
						searchBundle.put(key.substring(1), value);
						if(logger.isDebugEnabled()) logger.info("####searchBundle get from getAttribute and put("+key.substring(1)+","+value+")");
					}
				}
			}
		}
		return searchBundle;
	}
	@Override
	public void init(FilterConfig config) throws ServletException {
		String tmp = config.getInitParameter("isWebApp");
		if(tmp!=null && tmp.trim().equalsIgnoreCase("true")){
			isWebApp = true;
			logger.info("####isWebApp=true");
		}
		tmp = config.getInitParameter("charset");
		if(tmp!=null && !tmp.trim().isEmpty()){
			charset = tmp;
		}
	}
	

}
